package view.toolboxComponents;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import controller.CustomEvent;
import controller.EventHandler;
import controller.ToolBoxEventHandler;
/** * A class to encapsulate the width panel on the designer * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer * */
@SuppressWarnings("serial")
public class PrescriptionWidthPanel extends AbstractPanel{
	private JLabel lblPresWidth;
	private JTextField txtPresWidth;
	private EventHandler eventHandler;

	public PrescriptionWidthPanel(){
		super();
		txtPresWidth = new JTextField();
		txtPresWidth.setMaximumSize(new Dimension(120, 20));
		lblPresWidth = new JLabel("Width");
		buildPanel();
		txtPresWidth.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					CustomEvent event = new CustomEvent("presWidthChange", PrescriptionWidthPanel.this, txtPresWidth.getText());
					PrescriptionWidthPanel.this.eventHandler = new ToolBoxEventHandler();
					eventHandler.createEvent(event);
				}
			}			
			@Override
			public void keyReleased(KeyEvent e) {
			}
			@Override
			public void keyTyped(KeyEvent arg0) {
			}
		});
	}
	/**
	 * Set the text in the text field to specified text
	 * @param text the text to set as the width of the prescription
	 */
	public void setText(String text){
		txtPresWidth.setText(text);
	}
	/**
	 * Build prescription name panel
	 */
	private void buildPanel() {
		this.add(lblPresWidth);
		makeRigidArea();
		this.add(txtPresWidth);
	}	@Override
	public void resetComponent(){
		txtPresWidth.setText("");
	}
}